<?php
/* 
 * @todo store the html of each page so we can compare the current page with the stored page
 * You have no way of knowing when the page was changed if you don't store it.
 */




include_once "classes/PHP_Curl_Crawler.php";
include_once "classes/SpiderDB.php";

/**
 * Class to create a sitemap for your site
 * <code>
 * $sitemap = new Sitemap($url);
 * $sitemap->generateSitemap();
 * $sitemap->writeSitemap("sitemap.xml");
 * $submit = $sitemap->pingSearchEngines($url.'sitemap.xml');
 * foreach($submit as $searchengine){
 *     echo $searchengine[0], " -> ",$searchengine[1], "<br/>\n";
 * }
 * </code>
 * @author Yves Peeters
 */
class Sitemap {
    private $url="";
    private $data="";
    private $xml;
    private $ping=
    array(array('Google','http://www.google.com/webmasters/sitemaps/ping?sitemap='),
          array('Yahoo','http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=YahooDemo&url='),
          array('Ask','http://submissions.ask. com/ping?sitemap='),
          array('Bing','http://www.bing.com/webmaster/ping.aspx?siteMap='));
    public function __consturct(){
    }

    /**
     * fetch your good urls from the database
     * {@source}
     */
    private function getData(){
        $db = new SpiderDB();
        $this->data = $db->getGoodUrls();
    }

    /**
     * generate xml sitemap and store it in $xml
     * {@source}
     */
    public function generateSitemap(){
        $this->getData();
        $this->xml = "<?xml version='1.0' encoding='UTF-8'?>\n
            \t<urlset xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
         xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'
         xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>";
        foreach($this->data as $url){
            $this->xml .="\t\t<url>\n";
            $this->xml .="\t\t\t<loc>".urlencode($url)."</loc>\n";
            $this->xml .="\t\t</url>";
        }
        $this->xml .="\t</urlset>";
    }

    /**
     * Writes the sitemap file to a specific path
     * <code>
     * $sitemap->writeSitemap("sitemap.xml");
     * </code>
     * {@source}
     * @param String $path
     */
    public function writeSitemap($path){
        $fp = fopen($path, 'w');
        fwrite($fp, $this->xml);
    }

    /**
     * Tell the search engines you have a new sitemap
     * <code>
     * $url = "www.mydomain.com";
     * $sitemap->pingSearchEngines($url.'sitemap.xml');
     * </code>
     * {@source}
     * @param String $url
     * @return array
     */
    public function pingSearchEngines($url){
        $return = array();
        foreach ($this->ping as $SE){
            $crl = new PHP_Curl_Crawler();
            $crl->init();
            $response = $crl->getContent($SE[1].$url);
            array_push($return,array($SE[0],$response));
            unset($crl);
        }
        return $return;
    }
}
?>
